<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="分类" prop="sid">
        <el-select class="!w-full" v-model="formData.sid" clearable placeholder="请选择分类">
          <el-option
            v-for="foodType in foodTypeList"
            :key="foodType.id"
            :label="foodType.name"
            :value="foodType.sid"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="食物名称" prop="foodName">
        <el-input v-model="formData.foodName" placeholder="请输入食物名称" />
      </el-form-item>
      <el-form-item label="真实名称" prop="trueName">
        <el-input v-model="formData.trueName" placeholder="请输入真实名称" />
      </el-form-item>
      <el-form-item label="可食用部分比例（%）" prop="foodPart">
        <el-input v-model="formData.foodPart" placeholder="请输入可食用部分比例（%）" />
      </el-form-item>
      <el-form-item label="水分（g/100g）" prop="water">
        <el-input v-model="formData.water" placeholder="请输入水分（g/100g）" />
      </el-form-item>
      <el-form-item label="能量（kcal）" prop="energyKcal">
        <el-input v-model="formData.energyKcal" placeholder="请输入能量（kcal）" />
      </el-form-item>
      <el-form-item label="蛋白质（g）" prop="proteinG">
        <el-input v-model="formData.proteinG" placeholder="请输入蛋白质（g）" />
      </el-form-item>
      <el-form-item label="脂肪（g）" prop="fatG">
        <el-input v-model="formData.fatG" placeholder="请输入脂肪（g）" />
      </el-form-item>
      <el-form-item label="碳水化合物（g）" prop="carbohydrateG">
        <el-input v-model="formData.carbohydrateG" placeholder="请输入碳水化合物（g）" />
      </el-form-item>
      <el-form-item label="膳食纤维（g）" prop="fiberG">
        <el-input v-model="formData.fiberG" placeholder="请输入膳食纤维（g）" />
      </el-form-item>
      <el-form-item label="维生素A（μg）" prop="vitaminAUg">
        <el-input v-model="formData.vitaminAUg" placeholder="请输入维生素A（μg）" />
      </el-form-item>
      <el-form-item label="视黄醇当量（μg）" prop="retinolEquivalentUg">
        <el-input v-model="formData.retinolEquivalentUg" placeholder="请输入视黄醇当量（μg）" />
      </el-form-item>
      <el-form-item label="维生素B1（mg）" prop="vitaminB1Mg">
        <el-input v-model="formData.vitaminB1Mg" placeholder="请输入维生素B1（mg）" />
      </el-form-item>
      <el-form-item label="维生素B2（mg）" prop="vitaminB2Mg">
        <el-input v-model="formData.vitaminB2Mg" placeholder="请输入维生素B2（mg）" />
      </el-form-item>
      <el-form-item label="尼克酸（mg）" prop="niacinMg">
        <el-input v-model="formData.niacinMg" placeholder="请输入尼克酸（mg）" />
      </el-form-item>
      <el-form-item label="维生素C（mg）" prop="vitaminCMg">
        <el-input v-model="formData.vitaminCMg" placeholder="请输入维生素C（mg）" />
      </el-form-item>
      <el-form-item label="维生素E（mg）" prop="vitaminEMg">
        <el-input v-model="formData.vitaminEMg" placeholder="请输入维生素E（mg）" />
      </el-form-item>
      <el-form-item label="β-胡萝卜素（μg）" prop="betaCaroteneUg">
        <el-input v-model="formData.betaCaroteneUg" placeholder="请输入β-胡萝卜素（μg）" />
      </el-form-item>
      <el-form-item label="钙（mg）" prop="calciumMg">
        <el-input v-model="formData.calciumMg" placeholder="请输入钙（mg）" />
      </el-form-item>
      <el-form-item label="磷（mg）" prop="phosphorusMg">
        <el-input v-model="formData.phosphorusMg" placeholder="请输入磷（mg）" />
      </el-form-item>
      <el-form-item label="钾（mg）" prop="potassiumMg">
        <el-input v-model="formData.potassiumMg" placeholder="请输入钾（mg）" />
      </el-form-item>
      <el-form-item label="钠（mg）" prop="sodiumMg">
        <el-input v-model="formData.sodiumMg" placeholder="请输入钠（mg）" />
      </el-form-item>
      <el-form-item label="镁（mg）" prop="magnesiumMg">
        <el-input v-model="formData.magnesiumMg" placeholder="请输入镁（mg）" />
      </el-form-item>
      <el-form-item label="铁（mg）" prop="ironMg">
        <el-input v-model="formData.ironMg" placeholder="请输入铁（mg）" />
      </el-form-item>
      <el-form-item label="锌（mg）" prop="zincMg">
        <el-input v-model="formData.zincMg" placeholder="请输入锌（mg）" />
      </el-form-item>
      <el-form-item label="硒（mg）" prop="seleniumMg">
        <el-input v-model="formData.seleniumMg" placeholder="请输入硒（mg）" />
      </el-form-item>
      <el-form-item label="铜（mg）" prop="copperMg">
        <el-input v-model="formData.copperMg" placeholder="请输入铜（mg）" />
      </el-form-item>
      <el-form-item label="锰（mg）" prop="manganeseMg">
        <el-input v-model="formData.manganeseMg" placeholder="请输入锰（mg）" />
      </el-form-item>
      <el-form-item label="碘（μg）" prop="iodineUg">
        <el-input v-model="formData.iodineUg" placeholder="请输入碘（μg）" />
      </el-form-item>
      <!-- <el-form-item label="排序号" prop="sort">
        <el-input v-model="formData.sort" placeholder="请输入排序号" />
      </el-form-item> -->
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="formData.status">
          <el-radio
            v-for="dict in getIntDictOptions(DICT_TYPE.COMMON_STATUS)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { RecipeFoodApi, RecipeFood } from '@/api/school/recipe/food'
import { RecipeFoodTypeApi, RecipeFoodType } from '@/api/school/recipe/foodType'

/** 食物管理 表单 */
defineOptions({ name: 'RecipeFoodForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  sid: undefined,
  foodId: undefined,
  foodName: undefined,
  trueName: undefined,
  foodPart: undefined,
  water: undefined,
  energyKcal: undefined,
  proteinG: undefined,
  fatG: undefined,
  carbohydrateG: undefined,
  fiberG: undefined,
  vitaminAUg: undefined,
  retinolEquivalentUg: undefined,
  vitaminB1Mg: undefined,
  vitaminB2Mg: undefined,
  niacinMg: undefined,
  vitaminCMg: undefined,
  vitaminEMg: undefined,
  betaCaroteneUg: undefined,
  calciumMg: undefined,
  phosphorusMg: undefined,
  potassiumMg: undefined,
  sodiumMg: undefined,
  magnesiumMg: undefined,
  ironMg: undefined,
  zincMg: undefined,
  seleniumMg: undefined,
  copperMg: undefined,
  manganeseMg: undefined,
  iodineUg: undefined,
  sort: undefined,
  status: 0
})
const formRules = reactive({
  sid: [{ required: true, message: '分类码不能为空', trigger: 'blur' }],
  // foodId: [{ required: true, message: '食物编码不能为空', trigger: 'blur' }],
  foodName: [{ required: true, message: '食物名称不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref
const foodTypeList = ref([] as RecipeFoodType[]) //食物分类列表

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await RecipeFoodApi.getRecipeFood(id)
    } finally {
      formLoading.value = false
    }
  }

  //查询食物分类列表
  foodTypeList.value = await RecipeFoodTypeApi.getSimpleRecipeFoodType()
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as RecipeFood
    if (formType.value === 'create') {
      await RecipeFoodApi.createRecipeFood(data)
      message.success(t('common.createSuccess'))
    } else {
      await RecipeFoodApi.updateRecipeFood(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    sid: undefined,
    foodId: undefined,
    foodName: undefined,
    trueName: undefined,
    foodPart: undefined,
    water: undefined,
    energyKcal: undefined,
    proteinG: undefined,
    fatG: undefined,
    carbohydrateG: undefined,
    fiberG: undefined,
    vitaminAUg: undefined,
    retinolEquivalentUg: undefined,
    vitaminB1Mg: undefined,
    vitaminB2Mg: undefined,
    niacinMg: undefined,
    vitaminCMg: undefined,
    vitaminEMg: undefined,
    betaCaroteneUg: undefined,
    calciumMg: undefined,
    phosphorusMg: undefined,
    potassiumMg: undefined,
    sodiumMg: undefined,
    magnesiumMg: undefined,
    ironMg: undefined,
    zincMg: undefined,
    seleniumMg: undefined,
    copperMg: undefined,
    manganeseMg: undefined,
    iodineUg: undefined,
    sort: undefined,
    status: 0
  }
  formRef.value?.resetFields()
}
</script>
